package com.example.demo2.dao.impl;

import com.example.demo2.dao.UserDao;
import com.example.demo2.model.User;
import com.example.demo2.util.DBUtil;
import com.example.demo2.util.HikariUtil;

import java.sql.*;

public class UserDaoImpl implements UserDao {

    @Override
    public User findByUsername(String name) {


        String sql = "select * from user where username = ?";
        User user = new User();


        try (
                Connection conn = HikariUtil.getConnection();
                PreparedStatement prep = conn.prepareStatement(sql);

        ) {
            prep.setString(1, name);
            try (ResultSet rs = prep.executeQuery()) {
                while (rs.next()) {
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setEmail(rs.getString("email"));
                    user.setRole(rs.getString("role"));
                    user.setPhone(rs.getString("phone"));
                    user.setCreate_Time(
                            rs.getTimestamp("create_time")
                                    .toLocalDateTime()
                    );
                    user.setStatus(rs.getInt("status"));

                }
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
